package com.headcaselabs.orchestra.test;

import org.apache.log4j.PropertyConfigurator;

import com.headcaselabs.orchestra.Configuration;
import com.headcaselabs.orchestra.JobDaemon;
import com.headcaselabs.orchestra.ReportDaemon;
import com.headcaselabs.queue.SQSUtils;
import com.headcaselabs.work.TestUnitOfWork;

public class SampleTaskManager {

	public final static String CONF_FILE = "conf.xml";
	public static SQSUtils utils;
	
	public static void main(String args[]) {
		PropertyConfigurator.configure("log4j.properties") ;
		Configuration conf = Configuration.getConfiguration(CONF_FILE);
		utils = new SQSUtils(conf);
		System.out.println("Initialized configuration!");
		JobDaemon jd = JobDaemon.buildNewInstance(utils, conf);
		ReportDaemon rd = new ReportDaemon(120000,utils,new Object());
		String queues[] = new String[conf.getQueues().size()];
		conf.getQueues().toArray(queues);
		for (String s : queues) {
			System.out.println("Initialized queue:"+s);
		}
		injectJobs(10, queues);
		Thread t1 = new Thread(jd);
		Thread t2 = new Thread(rd);
		t1.start();
		t2.start();
	}
	
	public static void injectJobs(int num, String[] queues) {
		for (int i = 0; i < num; i++) {
			int ran_num = (int)(Math.random()* 100000);
			TestUnitOfWork j = new TestUnitOfWork(ran_num,"sample_"+ran_num,5000);
			j.setCreationTime(System.currentTimeMillis());
			String selectedQueue = queues[(int)(Math.random()*queues.length)];
			System.out.println("Sending via queue:"+selectedQueue);
			j.setQueueName(selectedQueue);
			utils.sendJob(j);
		}
	}

}
